<?php

namespace app\index\controller\hotel;

use app\common\controller\Hotel;
use think\Db;
use think\Exception;
use think\exception\PDOException;

use fast\Tree;

/**
 * 店铺管理
 * @internal
 */
class Shop extends Hotel
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    /**
     * Shop模型对象
     */
    protected $model = null;
    
    public function _initialize()
    {
        parent::_initialize();
        
        
        $this->model = new \app\admin\model\hotel\Hotel();



    }
    


    /**
     * 店铺资料
     */
    public function profile($ids = null)
    {
        $row = $this->model->get($this->shop->hotel_id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        // 判断用户权限
        if ($row['user_id'] !=$this->auth->id) {
            $this->error(__('You have no permission'));
        }
        if ($this->request->isPost()) {
			// 1.1.9升级 还原屏蔽html
			$this->request->filter([]);
            $params = $this->request->post("row/a");
            if ($params) {
                $result = false;
                Db::startTrans();
                try {
                    if (isset($params['lng'])&&isset($params['lat'])){
                        $res = $this->model->getcity($params['lat'],$params['lng']);
                        if ($res['code']!=1){
                            Db::rollback();
                            $this->error($res['msg']);
                        }
                        $params['province_id'] = $res['data']['province_id'];
                        $params['city_id'] = $res['data']['city_id'];
                        $params['district_id'] = $res['data']['district_id'];
                    }
					// 1.1.9升级优化 更新指定字段
					$result = $row
						->allowField(['image','hotel_name','mobile','reception','explain','address','lat','lng',
                            'province_id','city_id','district_id'])
						->save($params);
                    Db::commit();
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    
    /**
     * 图片空间
     */
    public function attachment()
    {
        $attachment = model('Attachment');
        $this->view->assign("picCount", $attachment->where('user_id', $this->auth->id)->count());
        $size = $attachment->where('user_id', $this->auth->id)->sum('filesize');
        $units = array('K','Kb','M','G','T');
        $i = 0;
        for (; $size>=1024 && $i<count($units); $i++) {
            $size /= 1024;
        }
        $this->view->assign("picSum", round($size, 2).$units[$i]);
		$this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
        return $this->view->fetch('hotel/attachment/index');
    }
    

	
}
